package com.woniu.service.impl;

import com.woniu.constant.SysConstant;
import com.woniu.dao.MenusMapper;
import com.woniu.dao.RolesMenusMapper;
import com.woniu.dto.MenusDTO;
import com.woniu.exception.MenuServiceException;
import com.woniu.pojo.Menus;
import com.woniu.pojo.RolesMenusKey;
import com.woniu.service.MenusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MenusServiceImpl implements MenusService {

    @Autowired
    private MenusMapper menusMapper;

    @Autowired
    private RolesMenusMapper rolesMenusMapper;

    @Override
    public List<MenusDTO> findMenusByRoles(Integer roleId) {
        //根据角色id查询当前角色能够访问的菜单资源
        List<Menus> menus = menusMapper.selectMenusByRoles(roleId);
        System.out.println("查询到的数据条数："+menus.size());
        List<MenusDTO> result = new ArrayList();
        for (Menus menu : menus) {
            if(menu.getParentId().equals(SysConstant.MEUNS_ROOT_ID)){
                MenusDTO dto = new MenusDTO();
                dto.setTitle(menu.getTitle());
                dto.setHref(menu.getHref());
                dto.setIcon(menu.getIcon());
                dto.setSpread(menu.getSpread());
                List childrens = new ArrayList();//存储子节点的结合
                for (Menus childMenu : menus) {//查找当前节点下的子节点
                    if(childMenu.getParentId().equals(menu.getMenuId())){
                        //封装子节点对象
                        MenusDTO child = new MenusDTO();
                        child.setSpread(childMenu.getSpread());
                        child.setIcon(childMenu.getIcon());
                        child.setHref(childMenu.getHref());
                        child.setTitle(childMenu.getTitle());
                        childrens.add(child);
                    }
                }
                dto.setChildren(childrens);
                result.add(dto);
            }
        }
        return result;
    }

    @Override
    public List<Menus> findAll() {
        return menusMapper.selectAll();
    }

    @Override
    public List<Menus> findCheckMenusByRoleId(Integer id) {
        List<Menus> all = menusMapper.selectAll();
        //默认拥有的权限
        List<Menus> menus = menusMapper.selectMenusByRoles(id);
        for (Menus m : all) {
            for (Menus menu : menus) {
                if(m.getMenuId().equals(menu.getMenuId())){
                    m.setChecked("true");
                }
            }
        }
        return all;
    }
    @Override
    public boolean checkMenuName(Menus menus) {
        List<Menus> dbMenus = menusMapper.selectByTitle(menus.getTitle());
        if(dbMenus == null || dbMenus.size()==0){
            return false;
        }
        for (Menus dbMenu : dbMenus) {
            if(dbMenu.getParentId().equals(menus.getParentId())){
                return true;
            }
        }

        return false;
    }

    @Override
    public void saveMenus(Menus menus) {
        menus.setSpread("false");
        menusMapper.insertSelective(menus);
    }


    @Override
    public void delMenus(Integer mid) {
        //检查该节点下是否存在子节点
        Menus child = menusMapper.findByParentId(mid);
        if (child != null) {
            throw new MenuServiceException("该节点下存在子节点");
        }
        //该菜单节点是否已经被分配个其它的角色
        List<RolesMenusKey> list = rolesMenusMapper.selectByMenuId(mid);
        if(list.size()>0){
            throw new MenuServiceException("该菜单已经被分配给角色");
        }

        menusMapper.deleteByPrimaryKey(mid);
    }

    @Override
    public void updateMenusSorting(Integer menuId, Integer sorting) {
        menusMapper.updateSorting(menuId,sorting);
    }

    @Override
    public Menus findByMenuId(Integer menuId) {
        return menusMapper.findByMenuId(menuId);
    }

    @Override
    public void updateMenus(Menus menus) {
        menus.setSpread("false");
        menusMapper.updateByPrimaryKey(menus);
    }

}
